package com.apes.autoparts.searcher.model;

import lombok.Getter;
import lombok.Setter;

import java.util.*;

@Getter
@Setter
public class EpcPartNumberParse {

    public static final String CONST_TABLE_NAME = "epc_partnumber_parse";


    private String id;

    private String epcVersion;

    private String partChineseName;

    private String menuName;

    private String state;

    private Map _creator;

    private Map approveId;

  private List<EpcPartNumberParseList> epcPartNumberParseList = new ArrayList();

    public List<EpcPartNumberParseList> getEpcPartNumberParseList() {
        if( epcPartNumberParseList == null ) return new ArrayList();
        for (EpcPartNumberParseList item : epcPartNumberParseList) {
            item.setEpcPartNumberParse(this);
        }
        return epcPartNumberParseList;
    }

    /***
     * 对解析结果去重并返回
     * @return
     */
    public Map<String, Set<String>> uniquePartNumberParse() {
        List<EpcPartNumberParseList> epcPartNumberParseLists = getEpcPartNumberParseList();
        Map<String, Set<String>> result = new HashMap();
        epcPartNumberParseLists.stream().forEach(epcPartNumberParseList -> {
            String partNumber = epcPartNumberParseList.getPartNumber();
            Set<String> parseSet = new HashSet();
            Set<String> stringSet = result.get(partNumber) != null ? result.get(partNumber) : new HashSet(); // 获取缓存数据
            if( epcPartNumberParseList.getPartDescription() != null ) {
                parseSet.addAll( Arrays.asList(epcPartNumberParseList.getPartDescription().split(";"))); // 获取解析后的数据
            }
            parseSet.addAll(stringSet); // 利用Set进行去重
            result.put(partNumber, parseSet);
        });
        return result;
    }

}


